/* 
 * File:   Main.cpp
 * Author: Guilherme Monteiro
 *
 */

#include "headers/Main.h"

Main::Main() {
}

Main::Main(const Main& orig) {
}

Main::~Main() {
}

/**
 * Main function of TesseractExecutorApp.
 * 
 * @param argc
 * @param argv
 * @return 0 if all ends ok
 */
int main(int argc, char** argv) {
    if (argc == 2) {
        try {
            string highTablePath = argv[1];
            
            printf("argv[0]: %s\n", argv[0]);
            printf("argv[1]: %s\n", argv[1]);
            
            TessMetaInfo* meta = new TessMetaInfo(highTablePath);
            ImageFormatConverter* converter = new ImageFormatConverter(meta);
            converter->convertToTiffFormat();

            Reader* reader = new Reader(meta->getUserAnswerPath());
            vector<Cell> cells = reader->readCells();

            GenericAdapter* genericAdapter = new GenericAdapter(LOW_2_HIGH, meta);
            vector<Cell> cellsAdapted = genericAdapter->adaptCells(cells);

            TesseractExecutor* executor = new TesseractExecutor(cellsAdapted, meta->getTIFFImagePath());
            vector<Transcription*> transcriptions = executor->runTesseract();

            //        printf("transcriptions.size(): %d\n", (int)transcriptions.size());

            File* f1 = new File(meta->getOutputTextsFileName());
            File* f2 = new File(meta->getOutputConfidencesFileName());

            f1->writeHeader1("#0,0,0,0");
            f2->writeHeader1("#0,0,0,0");

            vector<Transcription*>::iterator itTransc;
            for (itTransc = transcriptions.begin(); itTransc != transcriptions.end(); itTransc++) {
                f1->writeHeaderN((*itTransc)->getText());

                std::ostringstream sstream;
                sstream << (*itTransc)->getMeanTextConfidence();
                std::string taxAsString = sstream.str();

                f2->writeHeaderN(taxAsString);
            }

            printf("====> TesseractExecutorApp executed with success.\n");

        } catch (NullImageException nie) {
            cerr << nie.what() << endl;
        } catch (InvalidFileException ife) {
            cerr << ife.what() << endl;
        }        catch (exception &e) {
            cerr << "Some error ocurred. Please, report the bug to guilherme.gadelha [at] ccc [dot] ufcg [dot] edu [dot] br" << endl;
        }
    } else {
        cerr << "Invalid number of arguments. Verify README to know more." << endl;
    }

    return 0;
}

